/*
 * @(#)XfpkgVectorPanel.java        2003/01/01
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//CLASE DE XFPKG QUE EDITA LOS VECTORES: ALIAS, PARAM, DEFINED	//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

package xfuzzy.xfpkg;

import xfuzzy.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;

/**
 * Clase de Xfpkg que edita los vectores: alias, param, defined
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfpkgVectorPanel extends JPanel implements ActionListener {

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603051L;

	/**
	 * N�mero m�nimo de campos a mostrar
	 */
	private int MINSIZE;
	
	/**
	 * Posici�n del campo que tiene la etiqueta "New"
	 */
	private int index;
	
	/**
	 * Componentes que referencian los campos editables
	 */
	private XTextForm text[];
	
	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//
	
	/**
	 * Constructor
	 */
	public XfpkgVectorPanel(int minsize) {
		super();
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		this.MINSIZE = minsize;
		this.index = 0;
		this.text = new XTextForm[minsize];
		for(int i=0; i<text.length; i++) {
			text[i] = new XTextForm(" ", this);
			text[i].setLabelWidth(100);
			text[i].setFieldWidth(200);
			text[i].setAlignment(JButton.CENTER);
			text[i].setActionCommand(""+i);
			add(text[i]);
		}
		setLabel();
	}
	
 	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Actualiza el contenido del panel con una lista de nombres
	 */
	public void setList(String[] list) {
		int size = list.length;
		index = size;
		ensureSize();
		setLabel();
		for(int i=0; i<size; i++) text[i].setText(list[i]);
		for(int i=size; i<text.length; i++) text[i].setText("");
	}
	
	/**
	 * Actualiza el contenido del panel con una lista de nombres
	 */
	public void set(Vector<String> v) {
		int size = v.size();
		index = size;
		ensureSize();
		setLabel();
		for(int i=0; i<size; i++) text[i].setText((String) v.elementAt(i));
		for(int i=size; i<text.length; i++) text[i].setText("");
	}

	/**
	 * Obtiene la lista de nombres introducida en el panel
	 */
	public String[] getList() {
		String[] list = new String[index];
		for(int i=0; i<index; i++) list[i] = text[i].getText();
		return list;
	}

	/**
	 * Obtiene la lista de nombres introducida en el panel
	 */
	public Vector<String> get() {
		Vector<String> v = new Vector<String>();
		for(int i=0; i<index; i++) v.addElement(text[i].getText());
		return v;
	}
	
	/**
	 * (Des)Habilita la edicion de los campos
	 */
	public void setEditable(boolean editable) {
		for(int i=0; i<index; i++) text[i].setEditable(editable);
		for(int i=0; i<text.length; i++) text[i].setLabelEnabled(editable);
	}

	/**
	 * Interfaz ActionListener
	 */
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		int i = Integer.parseInt(command);
		if(i > index ) return; 
		if(i == index ) { 
			index++;
			ensureSize();
			setLabel();
		}
		else {
			for(int j=i+1; j<=index; j++) text[j-1].setText(text[j].getText());
			index--;
			ensureSize();
			setLabel();
		}
	}
	
 	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//
	
	/**
	 * Actualiza el texto de los botones del panel
	 */
	private void setLabel() {
		for(int i=0; i<index; i++) {
			text[i].setLabel("Remove");
			text[i].setEditable(true);
			text[i].setFieldEnabled(true);
		}
		for(int i=index; i<text.length; i++) {
			text[i].setLabel(" ");
			text[i].setFieldEnabled(false);
			text[i].setEditable(false);
		}
		text[index].setLabel("New");
	}

	/**
	 * Asegura un numero de campos suficiente para la lista
	 */
	private void ensureSize() {
		while(index < text.length-1 && text.length > MINSIZE) {
			remove(text[text.length-1]);
			XTextForm at[] = new XTextForm[text.length-1];
			System.arraycopy(text,0,at,0,text.length-1);
			text = at;
		}
		for(int i=index-text.length; i>=0; i--) {
			XTextForm nt = new XTextForm(" ",this);
			nt.setLabelWidth(100);
			nt.setFieldWidth(200);
			nt.setAlignment(JButton.CENTER);
			nt.setActionCommand(""+text.length);
			add(nt);
			XTextForm at[] = new XTextForm[text.length+1];
			System.arraycopy(text,0,at,0,text.length);
			at[text.length] = nt;
			text = at;
		}
	}
}
